package com.lanyuan.controller.system;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lanyuan.annotation.SystemLog;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.entity.ProductFormMap;
import com.lanyuan.mapper.ProductMapper;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.Common;
import com.lanyuan.util.FileUploadUtil;
import com.lanyuan.util.Kit;

/**
 * 
 * @author 产品设置
 * @Email: mmm333zzz520@163.com
 * @version 3.0v
 */
@Controller
@RequestMapping("/product/")
public class ProductController extends BaseController {
	@Inject
	private ProductMapper productMapper;

	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/system/product/list";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String pageNow, String pageSize, String column, String sort) throws Exception {
		ProductFormMap productFormMap = getFormMap(ProductFormMap.class);
		productFormMap = toFormMap(productFormMap, pageNow, pageSize, productFormMap.getStr("orderby"));
		productFormMap.put("column", "createTime");
		productFormMap.put("sort", "desc");
		pageView.setRecords(productMapper.findUserPage(productFormMap));// 不调用默认分页,调用自已的mapper中findUserPage
		return pageView;
	}

	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/system/product/add";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "系统管理", methods = "组管理-新增产品")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity(HttpServletRequest request, MultipartFile file) throws Exception {
		ProductFormMap productFormMap = getFormMap(ProductFormMap.class, request);
		String path = request.getRealPath("/uploadFile/activity");
		String filename = "activity_" + Kit.formatToyyyy_MM_dd_HH_mm_ss_SSS(System.currentTimeMillis()) + "_"
				+ RandomStringUtils.randomAlphanumeric(5);
		String extensionName = FilenameUtils.getExtension(file.getOriginalFilename());
		if (!"".equals(extensionName)) {
			// 保存图片
			productFormMap.put("imgpath",
					"/uploadFile/activity/" + FileUploadUtil.savePicToPath(path, filename, extensionName, file));
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		productFormMap.put("createTime", df.format(new Date()));// new Date()为获取当前系统时间
		productMapper.addEntity(productFormMap);
		return "success";
	}

	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if (Common.isNotEmpty(id)) {
			model.addAttribute("product", productMapper.findbyFrist("id", id, ProductFormMap.class));
		}
		return Common.BACKGROUND_PATH + "/system/product/edit";
	}

	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "系统管理", methods = "组管理-修改产品")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request, MultipartFile file) throws Exception {
		ProductFormMap productFormMap = getFormMap(ProductFormMap.class, request);
		String path = request.getRealPath("/uploadFile/activity");
		String filename = "activity_" + Kit.formatToyyyy_MM_dd_HH_mm_ss_SSS(System.currentTimeMillis()) + "_"
				+ RandomStringUtils.randomAlphanumeric(5);
		String extensionName = FilenameUtils.getExtension(file.getOriginalFilename());
		if (!"".equals(extensionName)) {
			// 保存图片
			productFormMap.put("imgpath",
					"/uploadFile/activity/" + FileUploadUtil.savePicToPath(path, filename, extensionName, file));
		} else {
			productFormMap.put("imgpath", productFormMap.get("imgfile").toString());

		}
		productFormMap.remove("imgfile");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		productFormMap.put("createTime", df.format(new Date()));// new Date()为获取当前系统时间
		productMapper.editEntity(productFormMap);
		return "success";
	}

	@ResponseBody
	@RequestMapping("deleteEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "系统管理", methods = "组管理-删除产品")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		for (String id : ids) {
			productMapper.deleteByAttribute("id", id, ProductFormMap.class);
		}
		return "success";
	}
}